import {Component, OnInit} from '@angular/core';
import {LocalDataSource} from 'ng2-smart-table';
import {NbToastrService} from '@nebular/theme';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';
import {HttpClient} from '@angular/common/http';
import {SERVER_URL} from '../../config';
import {ReturnData} from '../produce-find/produce-find.component';

@Component({
  selector: 'app-manufacturer',
  templateUrl: './manufacturer.component.html',
  styleUrls: ['./manufacturer.component.css']
})

export class ManufacturerComponent implements OnInit {

  settings = {
    noDataMessage: '暂无数据',
    add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmCreate: true,
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmSave: true,
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number',
        editable: false,
      },
      Name: {
        title: '生产商名称',
        type: 'string',
      },
      Address: {
        title: '地址',
        type: 'string',
      },
      Tel: {
        title: '联系电话',
        type: 'string',
      },
      Lng: {
        title: '经度',
        type: 'string',
      },
      Lat: {
        title: '纬度',
        type: 'string',
      },
      qsCode : {
        title: '许可证编号',
        type: 'string',
      },
      qsStartTime: {
        title: '许可证生效',
        type: 'string',
      },
      qsEndTime: {
        title: '许可证失效',
        type: 'string',
      },
      Fcondition: {
        title: '状态',
        editable: false,
        filter: {
          type: 'list',
          config: {
            selectText: '选择状态',
            list: [
              {value: '审核通过', title: '审核通过'},
              {value: '审核未通过', title: '审核未通过'},
            ]
          }
        }
      }
    },
  };

  source: LocalDataSource = new LocalDataSource();
  manufacturers: any;

  constructor(private http: HttpClient, private toastrService: NbToastrService) {
    this.loadData();
  }

  async loadData() {
    const data = await this.http.get<ReturnData>(SERVER_URL + '?s=App.manufacturer.select').toPromise();
    this.manufacturers = data.data.code;
    this.source.load(this.manufacturers);
  }

  onDeleteConfirm(event) {
    if (window.confirm('确定删除?')) {
      event.confirm.resolve();
      console.log(event);
    } else {
      event.confirm.reject();
    }
  }

  onCreateConfirm(event) {
    const formData = new FormData();
    if (event.newData.Name && event.newData.Address && event.newData.Tel) {
      console.log(event);
      formData.append('Name', event.newData.Name);
      formData.append('Address', event.newData.Address);
      formData.append('Tel', event.newData.Tel);
      formData.append('Lat', event.newData.Lat);
      formData.append('Lng', event.newData.Lng);
      formData.append('qsCode',event.newData.qsCode);
      formData.append('qsStartTime',event.newData.qsStartTime);
      formData.append('qsEndTime',event.newData.qsEndTime);
      this.http.post<ReturnData>(SERVER_URL + '?s=App.manufacturer.insert', formData).subscribe(data => {
        console.log(data);
        if (data.ret === 200) {
          this.toastrService.show('创建成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          event.confirm.reject();
          this.toastrService.show('更新失败', '提示', {status: NbToastStatus.DANGER});
        }
      });
    }
  }

  onEditConfirm(event) {
    const formData = new FormData();
    if (event.newData.Name && event.newData.Address && event.newData.Tel) {
      formData.append('id', event.newData.id);
      formData.append('Name', event.newData.Name);
      formData.append('Address', event.newData.Address);
      formData.append('Tel', event.newData.Tel);
      formData.append('Lat', event.newData.Lat);
      formData.append('Lng', event.newData.Lng);
      formData.append('qsCode',event.newData.qsCode);
      formData.append('qsStartTime',event.newData.qsStartTime);
      formData.append('qsEndTime',event.newData.qsEndTime);
      this.http.post<ReturnData>(SERVER_URL + '?s=App.manufacturer.update', formData).subscribe(data => {
        console.log(data);
        if (data.data.code) {
          this.toastrService.show('创建成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          event.confirm.reject();
          this.toastrService.show('更新失败', '提示', {status: NbToastStatus.DANGER});
        }
      });
    }
  }

  ngOnInit() {
  }

}
